<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="/static/pear/component/pear/css/pear.css" />
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">

            <div class="layui-form-item">
                <label class="layui-form-label">角色名称</label>
                <div class="layui-input-block">
                    <input type="text" name="rolename" lay-verify="required" autocomplete="off" placeholder="请输入角色名称"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">角色类型</label>
                <div class="layui-input-block">
                    <select name="type" lay-verify="required">
                        <option value="2">普通管理员</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">权限节点</label>
                <div class="layui-input-block" id="menu">
                </div>
            </div>

            {:token_field()}
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
{include file="_Fragment/javascript" /}
<script>
    layui.use(['form', 'jquery', 'tree'], function() {
        let form = layui.form;
        let tree = layui.tree;
        let $ = layui.jquery;


        $.ajax({
            url: '/Role/getTree',
            contentType: 'application/json',
            type: 'get',
            success: function(result) {
                //开启复选框
                tree.render({
                    elem: '#menu'
                    ,data: result
                    ,showCheckbox: true
                    ,id: 'id' //定义索引
                });
            }
        })



        form.on('submit(user-save)', function(data) {
            let loading = layer.load();

            //获得选中的节点
            var checkData = tree.getChecked('id');
            var rules = recursive(checkData);
            var str = rules.join(",");

            data.field['rule'] = str;
            $.ajax({
                url: '/Role/save',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function(result) {
                    layer.close(loading);
                    if (result.status == 0) {
                        layer.msg(result.msg, {
                            icon: 1,
                            time: 1000
                        }, function() {
                            parent.layer.close(parent.layer.getFrameIndex(window
                                .name)); //关闭当前页
                            parent.layui.table.reload("user-table");
                        });
                    } else {
                        layer.msg(result.msg, {
                            icon: 2,
                            time: 1000
                        });
                    }
                }
            })
            return false;
        });
    })

    function recursive(arr) {

        // 先实现将所有id都push进一个数组中

        let idList = arr.reduce((p, c) => {

            // p 代表上一次的数据，用于下一次循环中使用，初始化为[]

            // c 代表当前遍历项

            // 把每一项的id push进去

            c.id && p.push(c.id)

            // 判断每一项中的children长度是否存在,并且也一个空数组,如果都满足,进行递归遍历push

            c.children && c.children.length && (p = p.concat(this.recursive(c.children)))

            return p

        }, [])

        // 进行去重

        let resultArr = []

        // 对id组成的数组进行遍历

        for (let i = 0; i < idList.length; i++) {

            // 判断每一项是否在新数组中，如果不在就push到新数组中

            if (!resultArr.includes(idList[i])) {

                resultArr.push(idList[i])

            }

        }

        // 对去重的数组进行从小到大排序,这里就使用冒泡排序算法来实现了

        for (let i = 0; i < resultArr.length; i++) {

            for (let j = 0; j < resultArr.length - 1 - i; j++) {

                // 判断前一项是否大于后面一项，如果大于就交换位置

                if (resultArr[j] > resultArr[j + 1]) {

                    // 交换位置

                    [resultArr[j], resultArr[j + 1]] = [resultArr[j + 1], resultArr[j]]

                }

            }

        }

        // 最后把处理好的数组return出去就大功告成!!

        return resultArr

    }
</script>
<script>
</script>
</body>
</html>
